package com.xt.purchasingsystem.service.impl.stockManagementServiceImpl;

import com.xt.purchasingsystem.dto.DFileDto;
import com.xt.purchasingsystem.mapper.DFileMapper;
import com.xt.purchasingsystem.mapper.SCellMapper;
import com.xt.purchasingsystem.model.DFile;
import com.xt.purchasingsystem.model.SCell;
import com.xt.purchasingsystem.service.stockManagementService.SafetyInventoryConfigurationManageService;
import com.xt.purchasingsystem.utils.PageBean;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import com.xt.purchasingsystem.utils.SerialNumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * 库存管理-->安全库存配置管理的所有业务实现类
 * Author: 老姚
 * Date:  2020/3/22 17:41
 */
@Service
@Transactional

public class SafetyInventoryConfigurationManageServiceImpl implements SafetyInventoryConfigurationManageService {
    @Autowired
    private DFileMapper dFileMapper;
    @Autowired
    private SCellMapper sCellMapper;

    @Override
    public Result queryAllDFileByUnallocatedInventory(DFileDto dFileDto) {
        int rowCount = dFileMapper.rowCountByUnallocatedInventory(dFileDto);
        PageBean pageBean = new PageBean(dFileDto.getPageSize(), dFileDto.getCurrNo(), rowCount);
        dFileDto.setCurrNo(pageBean.getStart());
        dFileDto.setPageSize(pageBean.getEnd());
        List<DFile> dFiles = dFileMapper.queryDFileByUnallocatedInventory(dFileDto);
        pageBean.setPageList(dFiles);
        return ResultFactory.buildSuccessResult("查询成功", pageBean);
    }

    @Override
    public Result addSafetyInventoryConfiguration(SCell sCell) {
        sCell.setStoreId(SerialNumberUtil.repertorySerNumber());
        int insert = sCellMapper.insert(sCell);
        DFile dFile=new DFile();
        dFile.setProductId(sCell.getProductId());
        dFile.setDesignCellTag("K001-1");
        dFileMapper.updateDesignCellTagByProductId(dFile);
        if(insert==1){
            return ResultFactory.buildSuccessResult("提交成功,需要复核");
        }else{
            return ResultFactory.buildFailResult("提交失败");
        }
    }

    @Override
    public Result getAllUnreviewedScellByPage(Integer currNo, Integer pageSize) {
        int rowCount = sCellMapper.rowCountByUnreviewed();
        PageBean pageBean=new PageBean(pageSize,currNo,rowCount);
        List<SCell> allUnreviewedScell = sCellMapper.getAllUnreviewedScell(pageBean.getStart(), pageBean.getEnd());
        pageBean.setPageList(allUnreviewedScell);
        return ResultFactory.buildSuccessResult("查询成功",pageBean);
    }

    @Override
    public Result reviewSafetyInventoryConfiguration(SCell sCell) {
        int i = sCellMapper.updateByPrimaryKeySelective(sCell);
        if(i==1){
            return ResultFactory.buildSuccessResult("复核成功");
        }else{
            return ResultFactory.buildFailResult("复核失败");
        }
    }


}
